package com.mgq.design.filter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * <b>功能：</b>测试类<br>
 * <b>Copyright mgq</b>
 * <li>版本&nbsp;&nbsp;&nbsp;&nbsp;修改日期&nbsp;&nbsp;&nbsp;&nbsp;部　　门&nbsp;&nbsp;&nbsp;&nbsp;作　者&nbsp;&nbsp;&nbsp;&nbsp;变更内容</li><hr>
 * <li>v1.0&nbsp;&nbsp;&nbsp;&nbsp;20220331&nbsp;&nbsp;营业后台&nbsp;&nbsp;&nbsp;&nbsp;马广奇&nbsp;&nbsp;&nbsp;&nbsp;创建类</li>
 * <br><br>
 *
 * @author 马广奇
 * @version 2022-03-31 09:13
 */
public class Test {
    public static void main(String[] args) {
        List<Person> persons = new ArrayList<Person>();

        persons.add(new Person("Robert","Male", "Single"));
        persons.add(new Person("John","Male", "Married"));
        persons.add(new Person("Laura","Female", "Married"));
        persons.add(new Person("Diana","Female", "Single"));
        persons.add(new Person("Mike","Male", "Single"));
        persons.add(new Person("Bobby","Male", "Single"));
        Criteria male = new CriteriaMale();
        Criteria female = new CriteriaFeMale();
        Criteria single = new CriteriaSingle();
        Criteria singleMale  = new AndCriteria(male, single);//单身男性
        Criteria singleOrFemale  = new OrCriteria(female, single);//单身或者女性


       /* System.out.println("Males: ");
        List<Person> collect = persons.stream().filter(i -> "male".equalsIgnoreCase(i.getGender())).collect(Collectors.toList());
        printPersons(male.meetCriteria(persons));
        printPersons(collect);

        System.out.println("\nFemales: ");
        printPersons(female.meetCriteria(persons));

        System.out.println("\nSingle Males: ");
        printPersons(singleMale.meetCriteria(persons));

        System.out.println("\nSingle Or Females: ");
        printPersons(singleOrFemale.meetCriteria(persons));*/

        test(HashMap::new,
                (i)->{
                    IntStream.rangeClosed(1,10).boxed().forEach(j->i.put("a"+j.toString(),j.toString()));
                    return true;
                },
                System.out::println,
                i->i.startsWith("a"));

    }

    public static void printPersons(List<Person> persons){
        for (Person person : persons) {
            System.out.println("Person : [ Name : " + person.getName()
                    +", Gender : " + person.getGender()
                    +", Marital Status : " + person.getMaritalStatus()
                    +" ]");
        }
    }

    public static void test(Supplier<? extends Map<String,String>> supplier, Function<Map<String,String>,Boolean> function,Consumer<String> consumer, Predicate<String> predicate) {
        Map<String, String> map = supplier.get();
        function.apply(map);
        map.keySet().stream().filter(predicate).forEach(consumer);
    }
}
